package com.har.modules.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.har.common.exception.RRException;
import com.har.common.utils.*;
import com.har.modules.web.entity.SmsEntity;
import com.har.modules.web.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短信验证
 *
 * @author maorui
 */
@Slf4j
@RestController
@RequestMapping("/sms/send")
public class SmsController {

    @Autowired
    private SmsService smsService;

    /**
     * 发送短信
     * @param mobile
     * @param code
     * @return
     */
    @RequestMapping(value = "/single")
    public R single(@RequestParam("mobile") Long mobile, @RequestParam("code") String code) {
        log.info("mobile>>>{}",mobile);
        log.info("code>>>{}",code);
        Map<String,String> map=new HashMap<>();
        if (null!=mobile&&null!=code){
            log.info("nCode>>>{}",MapUtils.maps.get("nCode"));
            if (!StringUtils.equalsIgnoreCase(code, MapUtils.maps.get("nCode"))) {  //忽略验证码大小写
                throw  new RRException("图形验证码错误，请重新获取");
            }
            //验证手机号
            if(!ValidatUtil.checkMobileNumber(mobile.toString())){
                throw  new RRException("请输入正确的手机号");
            }
            //防止全局变量maps内存溢出进行清除 同时让图形验证码失效
            MapUtils.maps.clear();
            //调用发送短信的接口并返回验证码以及发送成功的data
            map=SendSingle.send(String.valueOf(mobile));
            //保存手机号
            smsService.save(map.get("data"),mobile);
        }else {
            //先验证图形验证码 再验证手机号
            if(null==code||"".equals(code)){
                throw  new RRException("请先输入正确的图形验证码");
            }
            if(null==mobile){
                throw  new RRException("请输入正确的手机号");
            }
        }
        return R.ok().put("data",map.get("respData"));
    }

    /**
     * 注册短信验证
     *
     * @param mobile
     * @return
     */
    @RequestMapping(value = "/user")
    public R user(@RequestParam("mobile") Long mobile) throws ParseException {
        log.info("mobile>>>{}",mobile);
        Map<String,String> map=new HashMap<>();

        if (null!=mobile){
            //验证手机号
            if(!ValidatUtil.checkMobileNumber(mobile.toString())){
                throw  new RRException("请输入正确的手机号");
            }
            //防止频繁刷验证
            List<SmsEntity> list=smsService.selectList(
                    new EntityWrapper<SmsEntity>().eq("mobile",mobile).orderBy("create_time DESC"));
            if(!list.isEmpty()){
                Date day=new Date();
                //时间格式(yyyy-MM-dd HH:mm:ss)
                SimpleDateFormat df = new SimpleDateFormat(DateUtils.DATE_TIME_PATTERN);

                DateFormat dateFormat = new SimpleDateFormat(DateUtils.DATE_TIME_PATTERN);
                //上次发送时间
                Date dateTime1 = dateFormat.parse(df.format(list.get(0).getCreateTime()));

                log.info("lastSendTime>>>{}",df.format(list.get(0).getCreateTime()));

                if(null!=MapUtils.map.get("spaceTime")){
                    //防刷间隔时间
                    Date dateTime2=dateFormat.parse(df.format(MapUtils.map.get("spaceTime")));

                    log.info("spaceTime>>>{}",df.format(MapUtils.map.get("spaceTime")));
                    //当前时间
                    Date dateTime3 = dateFormat.parse(df.format(day));
                    log.info("currentTime>>>{}",df.format(day));
                    //比较时间
                    if(dateTime2.after(dateTime1)){
                        if(dateTime3.before(dateTime2)){
                            throw new RRException("已发送短信",400);
                        }
                    }
                }

            }
            //调用发送短信的接口并返回验证码以及发送成功的data
            map=SendSingle.sendCommon(String.valueOf(mobile));
            //保存手机号
            smsService.saveCommon(map.get("data"),mobile);
        }else {
            throw  new RRException("请输入正确的手机号");
        }
        return R.ok().put("data",map.get("respData"));
    }

}
